<?php
  /*
   +--------------------------------------------------------------------------
   |   phpBIZ v3.0  full version
   |   ========================================
   |   by taft@wjl.cn yejun@wjl.cn
   |   http://www.phpbiz.cn http://www.wjl.cn
   |   all rights reserved
   +---------------------------------------------------------------------------
   |
   |   > 最后修改日期：2005-4-30 2006-2-8
   |
   +--------------------------------------------------------------------------
  */
!class_exists('Debug') && exit('Forbidden');
$Reg = new Register;

class Register

{
	
  var $base_url;

  var $agreement ='';
  function Register()
	
  {  
	
    global $INCOME,$BIZ,$cache,$DB;
		
    $regcache = '';
    $cache_file=ROOT_PATH.'cache/reg_cache.php';
    if(!file_exists($cache_file)) require ROOT_PATH."source/Cache/reg_inc.php";
    require $cache_file;
    $this ->agreement= nl2br($regcache['reg_agreement']);
    if($regcache[reg_open]!=1) $BIZ -> error("停止注册");
    //--------------------------------------
    // Kill globals - globals bad, Homer good.
    //---------------------------------------

    $tmp_in = array_merge( $_GET, $_POST, $_COOKIE );
				
    foreach ( $tmp_in as $k => $v )
      {
	unset($$k);
      }
				
    //---------------------------------------

    switch( $INCOME[code] )
      {
      case '00':
	$INCOME['agree'] &&  $BIZ -> assign("code",$INCOME['code']);
	$this -> reg_form();
	break;
      case 'doReg':
	$this -> do_reg();
	break;
      default:
	$this -> reg_form();
      }	

    $BIZ -> output('Register.tpl');

  }

  function reg_form()

  {

    global $BIZ,$INCOME,$DB;

    /*ajax support */
    $XAJAX = new xajax('index.php?act=Reg&code=00&agree=1');
    $XAJAX -> registerFunction("checkuser");    
    //$XAJAX-> debugOn();
    $BIZ -> assign('xajax_javascript',$XAJAX->getJavascript('source/libs/xajax/'));
    $XAJAX -> processRequests();
    /* end ajax support*/

    $DB -> db_query("SELECT * FROM biz_seller_property ORDER BY level_id ASC");	
    while( $r=$DB -> db_fetch_row() )
      $level[]= array($r['level_id'],$r['property_name'],$r['buy_fee'] );
    $BIZ -> assign("agreement",$this ->agreement);
    $BIZ -> assign("level",$level);
    return;

  }

  function do_reg()
  {

    global $BIZ,$INCOME,$DB,$CONF,$_LEVEL;
    $local_reg = $INCOME[reg];

    foreach( array('seller_logaccount','seller_password','seller_email_address') as $item )
      {
		
	if( $local_reg[$item] == "" )
	  $BIZ -> error("必要项目没填完整");
      }
    if( $local_reg['seller_password'] != $INCOME['repassword'] )
      $BIZ -> error("两次输入密码不正确");

    /*auth code */
    session_start();
    if(md5($INCOME['acode'])!=$_SESSION['authcode'])
      $BIZ -> error("验证码错误！");
    else
      unset($_SESSION['authcode']);
    $fullspace = chr(161);
    if (strpos($local_reg['seller_logaccount'],$fullspace)!==false)
      {
	$BIZ -> error("注册帐号非法");
      }

    $DB -> db_query(" SELECT count(*) as ct FROM biz_seller WHERE seller_logaccount = '".$local_reg[seller_logaccount]."'");

    $r = $DB -> db_fetch_row();
    if( $r['ct'] ) $BIZ -> error("该用户名已被注册","?act=Reg");

    $local_reg[seller_password] = md5($local_reg[seller_password]);
    $local_reg['seller_check_status'] = $CONF['check_seller']?'uncheck':'checked';
    if( $_LEVEL[level][$INCOME['seller_property_id']-1][2] > 0 )
      {                                                      //收费用户需要认证
	$local_reg['seller_check_status'] = 'uncheck';  
      }
    //-------------------------------------------------------
    //构建用户数据
    //-------------------------------------------------------
    $DB -> db_query("SELECT free_money,order_allow_count,goods_valid_day,att_pic_total FROM biz_seller_property WHERE level_id=".$INCOME['seller_property_id']);
    $pd = $DB -> db_fetch_row();
    $sid = $DB -> db_form_insert( "biz_seller",$local_reg );
    $seller_data['property_id']=intval($INCOME['seller_property_id']);
    $seller_data['seller_id']=$sid;
    $seller_data['pic_allow'] =$pd['att_pic_total'];
    $did = $DB -> db_form_insert( "biz_seller_data",$seller_data);		
    $BIZ ->redirect_screen("您已经成功注册");
    return;
  }
}

function checkuser($var)
{
  global $DB;
  $objResponse = new xajaxResponse('GB2312');

  if ($var =='')
    return $objResponse;
	
  $DB -> db_query("SELECT * FROM biz_seller WHERE seller_logaccount='".$var."'");
  $num = $DB -> db_fetch_num();
  if( $num ==1)
    $mes = "该用户名已被注册";
  else if( $num == 0)
    $mes = "您可以使用该帐号注册";
  else 
    $mes = "查询出错！";

  $objResponse->addAssign('message','innerHTML',$mes);
  return $objResponse;
}
?>